package model;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public abstract class JavaArtifact {

	private String path;
	private JavaFileExtension extension;
	private ClassVersion classVersion;

	protected JavaArtifact(String path, JavaFileExtension extension) {
		this.path = path;
		this.extension = extension;
		this.classVersion = new ClassVersion(0, 0);
	}

	public abstract ClassVersion calculateClassVersion();
	
	public abstract ClassVersion calculateClassVersion(InputStream inputStream);
	
	public ClassVersion getClassVersion() {
		return classVersion;
	}

	protected void setClassVersion(ClassVersion classVersion) {
		if (this.classVersion.compareTo(classVersion) > 0) {
			this.classVersion = classVersion;
		}
	}

	public String getPath() {
		return path;
	}

	public JavaFileExtension getExtension() {
		return extension;
	}

	protected byte[] copyToBuffer(InputStream inputStream) {
		ByteArrayOutputStream buffer = new ByteArrayOutputStream(1024);
		try {
			int readSize = 0;
			byte[] kByte = new byte[1024];
			while((readSize = inputStream.read(kByte)) > 0) {
				buffer.write(kByte, 0, readSize);
			}
			
			return buffer.toByteArray();
		} catch (IOException e) {
			throw new InvalidJavaArtifactContentException("IO Exception");
		} finally {
			try {
				buffer.close();
			} catch (IOException e) {
				throw new InvalidJavaArtifactContentException("IO Exception");
			}
		}
	}
	
	@Override
	public String toString() {
		return "JavaArtifact [path=" + path + ", extension=" + extension
				+ ", classVersion=" + classVersion + "']";
	}

	public static void main(String[] args) {
		String s = "CAFEBABE";
	    int len = s.length();
	    byte[] data = new byte[len / 2];
	    
	    for (int i = 0; i < len; i += 2) {
	        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
	                             + Character.digit(s.charAt(i+1), 16));
	    }
	    
	    for (byte b : data) {
	    	System.out.print(b + " ");
		}
	    
	    try {
			String dir = "C:\\Users\\ttoacargil\\Desktop\\SoapMessage.class";
			InputStream inputStream = new FileInputStream(dir);
			
			data = new byte[4];
			inputStream.read(data);
			
			for (byte b : data) {
		    	System.out.print(b + " ");
			}
			
			inputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	    
	    
		
//		byte[] array = new byte[]{202,254,186,190};
//		if (array != null ) {
//			ByteBuffer byteBuffer = ByteBuffer.wrap(array);
//			System.out.println(byteBuffer.getInt());
//		}
	}
}
